﻿using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;


namespace DebIt.Persistence.Model
{
    [Table(Name = "LoanClients")]
    public class ClientLoanStatePo
    {
        #region variables

        [Column(IsVersion = true)]
        private Binary _version;
        private EntityRef<LoanPo> _loan;
        private EntityRef<ClientPo> _client;
        private DateTime? _timestampPaid;
        #endregion


        #region properties

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ClientLoanStateId { get; set; }

        [Column(CanBeNull = false)]
        public int LoanId { get; set; }

        [Column(CanBeNull = false)]
        public int ClientId { get; set; }

        [Column(CanBeNull = false)]
        public bool IsPaid { get; set; }

        [Column(CanBeNull = true, DbType = "DateTime")]
        public DateTime? TimestampPaid
        {
            get
            {
                if (_timestampPaid.HasValue)
                    return _timestampPaid.Value.ToLocalTime();
                return null;
            }
            set
            {
                if(value.HasValue)
                _timestampPaid = value.Value.ToUniversalTime();
            }
        }

        [Association(Storage = "_loan", ThisKey = "LoanId", IsForeignKey = true)]
        public LoanPo Loan
        {
            get { return _loan.Entity; }
            set
            {
                _loan.Entity = value;
            }
        }

        [Association(Storage = "_client", ThisKey = "ClientId", IsForeignKey = true)]
        public ClientPo Client
        {
            get { return _client.Entity; }
            set
            {
                _client.Entity = value;
            }
        }

        #endregion
    }
}

